#mapping-script
# -*- coding: ms949 -*-
import bots.transform as transform
import bots.usersys.mappings.utils as utils

def main(inn,out):
    
    ns1 = '{urn:kr:gov:kcs:data:standard:KCS_ErrorsInDeclarationSchemaModule:1:0:0}'
    ns2 = '{urn:kr:gov:kcs:common:standard:KCS_ReusableAggregateBusinessInformationEntitySchemaModule:1:0:0}'
    rootelement = ns1 + 'ErrorsInDeclaration'
    
    #Read from inn by XML order

    #ErrorsInDeclaration 
    ReceiptDateTime = inn.get({'BOTSID':rootelement,ns1+'ReceiptDateTime':None})
    
    #ErrorsInDeclaration/Document
    DocumentTypeCode = inn.get({'BOTSID':rootelement},{'BOTSID':ns1+'Document',ns2+'TypeCode':None})
    DocumentIssueDateTime = inn.get({'BOTSID':rootelement},{'BOTSID':ns1+'Document',ns2+'IssueDateTime':None})
    
    #ErrorsInDeclaration/PreviousCustomsDocument
    PreviousCustomsDocumentTypeCode = inn.get({'BOTSID':rootelement},{'BOTSID':ns1+'PreviousCustomsDocument',ns2+'TypeCode':None})
    PreviousCustomsDocumentReferenceID = inn.get({'BOTSID':rootelement},{'BOTSID':ns1+'PreviousCustomsDocument',ns2+'referenceID':None})
    
    #ErrorsInDeclaration/ReceiptAdditionalInformation
    ReceiptAdditionalInformationContent = inn.get({'BOTSID':rootelement},{'BOTSID':ns1+'ReceiptAdditionalInformation',ns2+'Content':None})
    
    #ErrorsInDeclaration/CustomsOffice
    CustomsOfficeCode = inn.get({'BOTSID':rootelement},{'BOTSID':ns1+'CustomsOffice',ns2+'OfficeCode':None})
    CustomsOfficeCodeID = inn.get({'BOTSID':rootelement},{'BOTSID':ns1+'CustomsOffice',ns2+'CodeID':None})

    out.ta_info['botskey'] = 'CUSAPE-5IP-' + PreviousCustomsDocumentReferenceID.strip()
    
    #Write to out by EDIFACT order
    
    out.put({'BOTSID':'UNH','0062':out.ta_info['reference']})
    out.put({'BOTSID':'UNH','S009.0065':'CUSAPE'})
    out.put({'BOTSID':'UNH','S009.0052':'S'})
    out.put({'BOTSID':'UNH','S009.0054':'93A'})
    out.put({'BOTSID':'UNH','S009.0051':'KE'})
            
    out.put({'BOTSID':'UNH'},{'BOTSID':'BGM','C002.1001':DocumentTypeCode})
    out.put({'BOTSID':'UNH'},{'BOTSID':'BGM','1004':PreviousCustomsDocumentReferenceID})
    
    out.put({'BOTSID':'UNH'},{'BOTSID':'DTM','C507.2005':'137','C507.2380':utils.edifactYYYYMMDDHHNNSS(DocumentIssueDateTime),'C507.2379':'202'})
    out.put({'BOTSID':'UNH'},{'BOTSID':'DTM','C507.2005':'148','C507.2380':utils.edifactYYYYMMDDHHNNSS(ReceiptDateTime),'C507.2379':'202'})
    
    out.put({'BOTSID':'UNH'},{'BOTSID':'CNT','C270.6069':'5BA','C270.6066':ReceiptAdditionalInformationContent})
    
    out.put({'BOTSID':'UNH'},{'BOTSID':'NAD','3035':'MS'})
    out.put({'BOTSID':'UNH'},{'BOTSID':'NAD','C082.3039':CustomsOfficeCodeID})
    out.put({'BOTSID':'UNH'},{'BOTSID':'NAD','C082.1131':'113'})
    out.put({'BOTSID':'UNH'},{'BOTSID':'NAD','C082.3055':'KCS'})
        
    out.put({'BOTSID':'UNH'},{'BOTSID':'NAD'},{'BOTSID':'CTA','C056.3413':CustomsOfficeCode})

    # Loop 1 ~ 99    
    #ErrorsInDeclaration/ErrorContent
    for erp in inn.getloop({'BOTSID':rootelement},{'BOTSID':ns1+'ErrorContent'}):
        ErrorContentErrorSectionID = erp.get({'BOTSID':ns1+'ErrorContent',ns2+'ErrorSectionID':None})
        ErrorContentErrorFirstErrorKeyID = erp.get({'BOTSID':ns1+'ErrorContent',ns2+'FirstErrorKeyID':None})
        ErrorContentErrorSecondErrorKeyID = erp.get({'BOTSID':ns1+'ErrorContent',ns2+'SecondErrorKeyID':None})
        ErrorContentErrorThirdErrorKeyID = erp.get({'BOTSID':ns1+'ErrorContent',ns2+'ThirdErrorKeyID':None})
        ErrorContentErrorError = erp.get({'BOTSID':ns1+'ErrorContent',ns2+'Error':None})
        ErrorContentErrorSubmissionGradeNumeric = erp.get({'BOTSID':ns1+'ErrorContent',ns2+'SubmissionGradeNumeric':None})
        
        lou = out.putloop({'BOTSID':'UNH'},{'BOTSID':'ERP'})
        lou.put({'BOTSID':'ERP','C701.1049':ErrorContentErrorSectionID})
        lou.put({'BOTSID':'ERP','C701.1049':ErrorContentErrorSectionID,'C701.1052':ErrorContentErrorFirstErrorKeyID})
        lou.put({'BOTSID':'ERP'},{'BOTSID':'FTX','4451':'AAE','C108.4440#1':ErrorContentErrorSecondErrorKeyID})
        lou.put({'BOTSID':'ERP'},{'BOTSID':'FTX','4451':'AAE','C108.4440#1':ErrorContentErrorSecondErrorKeyID,'C108.4440#2':ErrorContentErrorThirdErrorKeyID})
        lou.put({'BOTSID':'ERP'},{'BOTSID':'FTX','4451':'AAI','C108.4440#1':ErrorContentErrorSubmissionGradeNumeric})
        lou.put({'BOTSID':'ERP'},{'BOTSID':'FTX','4451':'AAI','C108.4440#1':ErrorContentErrorSubmissionGradeNumeric,'C108.4440#2':ErrorContentErrorSecondErrorKeyID})
        lou.put({'BOTSID':'ERP'},{'BOTSID':'FTX','4451':'AAO','C108.4440#1':ErrorContentErrorError})
    # End Loop
    
    out.put({'BOTSID':'UNH'},{'BOTSID':'DOC','C002.1001':PreviousCustomsDocumentTypeCode})
    
    out.put({'BOTSID':'UNH'},{'BOTSID':'UNT','0074':out.getcount()+1})
    out.put({'BOTSID':'UNH'},{'BOTSID':'UNT','0062':out.ta_info['reference']}) 
    
